package com.esoon.capital.cis;


import com.esoon.capital.cis.model.projectlist.ProjectType;
import com.esoon.capital.cis.model.projectlist.ProjectsType;
import com.esoon.capital.cis.util.CisUtil;
import com.esoon.capital.cis.util.XmlUtil;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableView;
import org.apache.commons.io.FileUtils;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

public class MyController implements Initializable {

    @FXML
    private TreeTableColumn<ProjectType, String> name;
    @FXML
    private TreeTableColumn<ProjectType, String> info;

    @FXML
    private TreeTableView<ProjectType> treeTableView;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        ProjectsType projectsType = null;
        try {
            projectsType = (ProjectsType) XmlUtil.xmlStrToOject(ProjectsType.class, FileUtils.readFileToString(CisUtil.getProjects(), "UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
            ;
        }

        List<TreeItem<ProjectType>> projectItemList = new ArrayList<>();
        for (ProjectType projectType :
                projectsType.getProject()) {
            TreeItem<ProjectType> projectItem = new TreeItem<ProjectType>(projectType);
            projectItemList.add(projectItem);
        }

        //Creating the root element
        final TreeItem<ProjectType> root =
                new TreeItem<>(new ProjectType());
        root.setExpanded(true);


        //Adding tree items to the root
        root.getChildren().setAll(projectItemList);

        //Creating a name
//        name = new TreeTableColumn<String,String>("Column");
        name.setPrefWidth(150);

        //Defining cell content
        name.setCellValueFactory((TreeTableColumn.CellDataFeatures<ProjectType, String> p) ->
                new ReadOnlyStringWrapper(p.getValue().getValue().getName()));
        info.setPrefWidth(150);

        //Defining cell content
        info.setCellValueFactory((TreeTableColumn.CellDataFeatures<ProjectType, String> p) ->
                new ReadOnlyStringWrapper(p.getValue().getValue().toString()));

        //Creating a tree table view
//        treeTableView = new TreeTableView<>(root);
        treeTableView.setRoot(root);
//        treeTableView.getColumns().add(name);
        treeTableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        treeTableView.getSelectionModel().setCellSelectionEnabled(true);
        treeTableView.setPrefWidth(500);
        treeTableView.setShowRoot(true);

    }
}



